﻿#include "StdAfx.h"
#include "okcoinParse.h"
#include "HttpClient.h"
#include <string>
#include <regex>

//把utf8转成unicode，再转成cstring  
void ConvertUTF8ToANSI(char* strUTF8,CString &strANSI)  //  
{  
    int nLen = ::MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,(LPCTSTR)strUTF8,-1,NULL,0);  //返回需要的unicode长度  
    WCHAR * wszANSI = new WCHAR[nLen+1];  
    memset(wszANSI, 0, nLen * 2 + 2);  
    nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen);    //把utf8转成unicode  
      
    nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL);        //得到要的ansi长度  
    char *szANSI=new char[nLen + 1];  
    memset(szANSI, 0, nLen + 1);  
    WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL);          //把unicode转成ansi  
    strANSI = szANSI;  
    delete wszANSI;  
    delete szANSI;  
}  
okcoinParse::okcoinParse(void)
{
	memset(deal,0,sizeof(dealData)*LISTLEN);
	memset(buy,0,sizeof(wantData)*LISTLEN);
	memset(sell,0,sizeof(wantData)*LISTLEN);
}


okcoinParse::~okcoinParse(void)
{
}

bool okcoinParse::parseDeal()
{
	CHttpClient x;
	CString final,temp,url;
	srand(time(NULL));

	url.Format("https://www.okcoin.com/marketRecentDealRefresh.do?symbol=1&rnd=%d",rand());
	
	temp=x.doGet(url);
	ConvertUTF8ToANSI(temp.GetBuffer(),final);
	temp.ReleaseBuffer();
		final.Replace("</tr>","\n");
	for(int i=0;i<final.GetLength();i++)
	{
		if(final.GetAt(i) == '<')
		{
			final.Delete(i);
			while(final.GetAt(i)!='>')
				final.Delete(i);
			final.Delete(i);
			if(i>0)
				i--;
		}
	}
	final.Remove(' ');
	final.Remove('\t');
	final.Replace("￥"," ");
	final.Replace("Ł"," ");
	//final.Replace("\r\n","");
	
	//AfxMessageBox(final);
	char *src = final.GetBuffer();
	char *p;
	p=strtok(src,"\n");
	if(p)
		p=strtok(NULL,"\n");
	int i=0;
	for(i=0;p;i++)
	{
		//AfxMessageBox(p);
		sscanf(p,"%s%f%f",deal[i].time,&deal[i].price,&deal[i].num);
		p=strtok(NULL,"\n");
	}
	i;
	final.ReleaseBuffer();
	return false;
}


bool okcoinParse::parseBuy()
{
	CHttpClient x;
	CString final,temp,url;
	srand(time(NULL));

	url.Format("https://www.okcoin.com/marketEntrustRefresh.do?symbol=1&rnd=%d",rand());
	
	temp=x.doGet(url);
	ConvertUTF8ToANSI(temp.GetBuffer(),final);
	temp.ReleaseBuffer();
	final.Replace("</tr>","\n");
	for(int i=0;i<final.GetLength();i++)
	{
		if(final.GetAt(i) == '<')
		{
			final.Delete(i);
			while(final.GetAt(i)!='>')
				final.Delete(i);
			final.Delete(i);
			if(i>0)
				i--;
		}
	}
	final.Remove(' ');
	final.Remove('\t');
	final.Replace("￥"," ");
	final.Replace("Ł"," ");
	final.Replace("("," ");
	final.Replace(")"," ");
	//final.Replace("\r\n","");
	
//	AfxMessageBox(final);
	char *src = final.GetBuffer();
	char *p;
	p=strtok(src,"\n");
	int i=0;
	char b[10]="买 ",s[10]="卖 ";
	for(;p;)
	{
		if(strstr(p,b)) //买
		{
			int i;
			float price,num;
			sscanf(p,"买 %d%f%f",&i,&price,&num);
			buy[i-1].price = price;
			buy[i-1].num = num;

		}
		if(strstr(p,s)) //卖
		{
			int i;
			float price,num;
			sscanf(p,"卖 %d%f%f",&i,&price,&num);
			sell[i-1].price = price;
			sell[i-1].num = num;

		}
		p=strtok(NULL,"\n");
	}
	final.ReleaseBuffer();
	return false;
}


dealData okcoinParse::getDeal(int x)
{
	return deal[x];
}


wantData okcoinParse::getBuy(int x)
{
	return buy[x];
}
wantData okcoinParse::getSell(int x)
{
	return sell[x];
}